package com.gitee.linzl.zuul.gateway.fiters;

import javax.servlet.http.HttpServletRequest;

import org.springframework.http.MediaType;

import com.netflix.zuul.context.RequestContext;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public abstract class AbstractJSONPreFilter extends AbstractDefaultPostFilter {

	/**
	 * 判断是否为JSON数据
	 */
	@Override
	public boolean shouldFilter() {
		RequestContext ctx = RequestContext.getCurrentContext();
		HttpServletRequest request = ctx.getRequest();
		if (request == null) {
			return false;
		}
		String contentType = request.getContentType();
		// Don't use this filter on GET method
		if (contentType == null) {
			return false;
		}
		MediaType mediaType = MediaType.valueOf(contentType);
		return MediaType.APPLICATION_JSON.includes(mediaType) || MediaType.APPLICATION_JSON_UTF8.includes(mediaType);
	}
}
